package com.example.demo.thread;

import cn.hutool.core.thread.ExecutorBuilder;
import cn.hutool.core.thread.RejectPolicy;
import cn.hutool.core.thread.ThreadFactoryBuilder;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.lang.management.ThreadMXBean;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 测试静态线程池生命周期
 *
 * @author Luodl
 */
public class Demo10 {

    //private static ThreadPoolExecutor executorBuilder;

    public static void main(String[] args) {

        while (true) {

            RuntimeMXBean runtimeBean = ManagementFactory.getRuntimeMXBean();
            String jvmName = runtimeBean.getName();
            System.out.println("JVM Name = " + jvmName);
            long pid = Long.valueOf(jvmName.split("@")[0]);
            System.out.println("JVM PID  = " + pid);
            ThreadMXBean bean = ManagementFactory.getThreadMXBean();

            System.out.println("线程总数为 = " + bean.getThreadCount());

            ThreadFactory threadFactory = ThreadFactoryBuilder.create().setNamePrefix("aaaaaa").build();
            ThreadPoolExecutor executorBuilder;

            executorBuilder = ExecutorBuilder.create()
                    .setCorePoolSize(2)
                    .setMaxPoolSize(10)
                    .setHandler(RejectPolicy.CALLER_RUNS.getValue())
                    .setThreadFactory(threadFactory)
                    .build();

            for (int i = 0; i < 10; i++) {
                executorBuilder.execute(new Task(i + "aa"));
            }

            executorBuilder.shutdown();
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (Exception e) {

            }
            System.out.println("线程总数为 = " + bean.getThreadCount());
            System.out.println("当前排队线程数:" + executorBuilder.getQueue().size());
            System.out.println("当前活动线程数:" + executorBuilder.getActiveCount());
            System.out.println("执行完成线程数:" + executorBuilder.getCompletedTaskCount());
            System.out.println("任务总数:" + executorBuilder.getTaskCount());
            System.out.println("线程池核心线程数:" + executorBuilder.getCorePoolSize());
            System.out.println("线程池最大线程数:" + executorBuilder.getMaximumPoolSize());
            System.out.println("线程池大小:" + executorBuilder.getPoolSize());


            ThreadFactory threadFactory1 = ThreadFactoryBuilder.create().setNamePrefix("bbbbbb").build();
            executorBuilder = ExecutorBuilder.create()
                    .setCorePoolSize(4)
                    .setMaxPoolSize(20)
                    .setHandler(RejectPolicy.CALLER_RUNS.getValue())
                    .setThreadFactory(threadFactory1)
                    .build();

            for (int i = 0; i < 10; i++) {
                executorBuilder.execute(new Task(i + "bb"));
            }

            executorBuilder.shutdown();
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (Exception e) {

            }

            System.out.println("线程总数为 = " + bean.getThreadCount());

            System.out.println("当前排队线程数:" + executorBuilder.getQueue().size());
            System.out.println("当前活动线程数:" + executorBuilder.getActiveCount());
            System.out.println("执行完成线程数:" + executorBuilder.getCompletedTaskCount());
            System.out.println("任务总数:" + executorBuilder.getTaskCount());
            System.out.println("线程池核心线程数:" + executorBuilder.getCorePoolSize());
            System.out.println("线程池最大线程数:" + executorBuilder.getMaximumPoolSize());
            System.out.println("线程池大小:" + executorBuilder.getPoolSize());

            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (Exception e) {

            }

        }


    }


    private static class Task implements Runnable {
        String name;

        public Task(String name) {
            this.name = name;
        }

        @Override
        public void run() {
            try {
                TimeUnit.MILLISECONDS.sleep(1000);
                System.out.println(Thread.currentThread().getName() + "task " + name + " is running");
                System.out.println("task " + name + " is over");
            } catch (Exception e) {

            }

            /*for (int i = 1; i <= 10; i++) {
                System.out.println("task " + name + " is running");
            }
            System.out.println("task " + name + " is over");*/
        }
    }
}
